import utime as time
from machine import Pin
from machine import UART
LED_ON  = 0
LED_OFF = 1

PIN_LED_R = 39  # PIO1_6
PIN_LED_G = 40  # PIO1_7
PIN_LED_B = 37  # PIO1_4

LED_RED = Pin(("led_red", PIN_LED_R), Pin.OUT_PP)
LED_GREEN = Pin(("led_green", PIN_LED_G), Pin.OUT_PP)
LED_BLUE = Pin(("led_blue", PIN_LED_B), Pin.OUT_PP)

UART_OBJ = UART(2, 115200) 


while True:
    uart_message = UART_OBJ.read()
    print(uart_message)
    if (uart_message == b'') :
        pass
    else:
        # 去除掉左右的空格
        uart_message = uart_message.strip()
        print(uart_message)
        mes = str(uart_message).split("|")

        led_type,switch = mes[1],mes[0]
        print("switch")
        print(switch)
        print("led_type")
        print(led_type)

        if 'on' in led_type:
            # 设置引脚状态
            led_value= 0
        elif 'off' in led_type:
            led_value= 1
        else:
            # 如果两者都不是, 则进入下一次循环
            UART_OBJ.write("Please Input [red/green/blue on/off]")
            print("error")
            print(led_type)
            continue
        if 'red' in switch :
            LED_RED.value(led_value)
        elif "green" in switch :
            LED_GREEN.value(led_value)
        elif 'blue' in switch :
            LED_BLUE.value(led_value)
        else:
            # 如果三者都不是, 则进入下一次循环
            UART_OBJ.write("Please Input [red/green/blue|on/off]")
            continue


    time.sleep(1)


import utime as time
from machine import Pin
from machine import UART
LED_ON = 0
LED_OFF = 1

# 设置引脚标识
PIN_LED_R = 39  # 红色RGB的引脚
PIN_LED_G = 40  # 绿色RGB的引脚
PIN_LED_B = 37  # 蓝色RGB的引脚

# 初始化引脚
LED_RED = Pin(("led_red", PIN_LED_R), Pin.OUT_PP)
LED_GREEN = Pin(("led_green", PIN_LED_G), Pin.OUT_PP)
LED_BLUE = Pin(("led_blue", PIN_LED_B), Pin.OUT_PP)

# 初始化UART, 使用UART2, 波特率为115200
UART_OBJ = UART(2, 115200)

# 上电死循环, 编写功能代码
while True:
   # 读取UART内容
   uart_message = UART_OBJ.read()
   print(uart_message)
   if (uart_message == b'') :
       # 如果为空内容, 不做任何处理
       pass
   else:
       # 去除掉左右的空格
       uart_message = uart_message.strip()
       print(uart_message)
       # 拆分字符串, 获得要控制的LED和开关状态
       mes = str(uart_message).split("|")
       led_type,switch = mes[1],mes[0]
       
       print("switch")
       print(switch)
       print("led_type")
       print(led_type)

       if 'on' in led_type:
           # 设置引脚状态
           led_value= 0
       elif 'off' in led_type:
           led_value= 1
       else:
           # 如果两者都不是, 则进入下一次循环, 并提示用户输入信息
           UART_OBJ.write("Please Input [red/green/blue on/off]")
           print("error")
           print(led_type)
           continue
       if 'red' in switch :
           LED_RED.value(led_value)
       elif "green" in switch :
           LED_GREEN.value(led_value)
       elif 'blue' in switch :
           LED_BLUE.value(led_value)
       else:
           # 如果三者都不是, 则进入下一次循环, 并提示用户输入信息
           UART_OBJ.write("Please Input [red/green/blue|on/off]")
           continue
   time.sleep(1)